﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 主角移动或旋转
/// </summary>
public class LocalPlayer : MonoBehaviour
{
    Tank tank;
    float lastFiretime;
    // Use this for initialization
    void Start()
    {
        tank = this.GetComponent<Tank>();
    }
    void Update()
    {
        if (Time.time - lastFiretime > 0.5f)
        {
            if (Input.GetMouseButton(0))
            {
                lastFiretime = Time.time;
                tank.Fire();
                SyncFire();
            }
        }
    }
    void FixedUpdate()
    {
        float horizon = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        tank.Move(vertical, horizon);
        SyncPosition();
    }
    void SyncPosition()
    {
        //发送自己的坐标位置
        Data.ReqPosition req = new Data.ReqPosition();
        req.username = tank.Account.username;
        req.position = Util.UV2PV(transform.position);
        req.forward = Util.UV2PV(transform.forward);

        NetworkManager.Instance.Send(Protocal.PlayerMove_CMD, req);
    }
    void SyncFire()
    {
        //发送自己的坐标位置
        Data.ReqPosition req = new Data.ReqPosition();
        req.username = tank.Account.username;
        req.position = Util.UV2PV(transform.position);
        req.forward = Util.UV2PV(transform.forward);

        NetworkManager.Instance.Send(Protocal.PlayerFire_CMD, req);
    }
}
